﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Hangjing.Common;
using Newtonsoft.Json;
using System.Text;
using System.IO;
using Hangjing.Model;
using Hangjing.DBUtility;
using Hangjing.SQLServerDAL;
using Hangjing.EleAPIV2;

using System.Collections.Specialized;


/// <summary>
/// 饿了么商家授权
/// </summary>
public partial class api_meituan_authcallback : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
     

        string error = Request["error"];

        if (error != null && error.Length > 0)
        {
            Response.Write("商户拒绝授权，原因：" + Request["error_description"]);
        }
        else {

            ELEAuth auth = new Hangjing.EleAPIV2.ELEAuth(Context);
            access_tokenInfo token = auth.getToken();
            if (token != null && token.expires_in > 0)
            {

                ELEShopV2 eleshop = new ELEShopV2(Context);
                ShopResult rs = eleshop.getUser(token.access_token);

                if (rs.error != null)
                {
                    Response.Write("商户授权失败");

                    return;
                }

                foreach (var item in rs.result.authorizedShops)
                {
                    taobaoAPIAcountInfo mm = new taobaoAPIAcountInfo();
                    mm.Parentid = 0;
                    mm.Status = token.shopid;
                    mm.isDel = 0;
                    mm.classname = item.name;
                    mm.linkurl = item.id.ToString();

                    mm.hovepic = token.refresh_token;
                    mm.pic = token.access_token;
                    mm.Depth = 2;
                    mm.Priority = token.expires_in;
                    mm.lasttime = DateTime.Now;

                    new taobaoAPIAcount().Add(mm);
                }



                Response.Write("授权成功，可以正常接单了");
            }
            else
            {
                Response.Write("授权失败，请重试");
            }

        }
    }
}